home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 176-200 / disk_195 / microemacs / src.zoo / ibmpc.c < prev    next >
C/C++ Source or Header  |  1989-03-23  |  12KB  |  524 lines

  1. /*
  2.  * The routines in this file provide support for the IBM-PC and other
  3.  * compatible terminals. It goes directly to the graphics RAM to do
  4.  * screen output. It compiles into nothing if not an IBM-PC driver
  5.  * Supported monitor cards include CGA, MONO and EGA.
  6.  */
  7.  
  8. #define    termdef    1            /* don't define "term" external */
  9.  
  10. #include        <stdio.h>
  11. #include    "estruct.h"
  12. #include    "etype.h"
  13. #include        "edef.h"
  14. #include    "elang.h"
  15.  
  16. #if     IBMPC
  17. #define NROW    50            /* Max Screen size.        */
  18. #define NCOL    80                      /* Edit if you want to.         */
  19. #define    MARGIN    8            /* size of minimim margin and    */
  20. #define    SCRSIZ    64            /* scroll size for extended lines */
  21. #define    NPAUSE    200            /* # times thru update to pause */
  22. #define BEL     0x07                    /* BEL character.               */
  23. #define ESC     0x1B                    /* ESC character.               */
  24. #define    SPACE    32            /* space character        */
  25.  
  26. #define    SCADC    0xb8000000L        /* CGA address of screen RAM    */
  27. #define    SCADM    0xb0000000L        /* MONO address of screen RAM    */
  28. #define SCADE    0xb8000000L        /* EGA/VGA address of screen RAM*/
  29.  
  30. #define MONOCRSR 0x0B0D            /* monochrome cursor        */
  31. #define CGACRSR 0x0607            /* CGA cursor            */
  32. #define EGACRSR 0x0709            /* EGA/VGA cursor        */
  33.  
  34. #define    CDCGA    0            /* color graphics card        */
  35. #define    CDMONO    1            /* monochrome text card        */
  36. #define    CDEGA    2            /* EGA color adapter        */
  37. #define    CDVGA    3            /* VGA color adapter        */
  38. #define    CDSENSE    9            /* detect the card type        */
  39.  
  40. #define NDRIVE    4            /* number of screen drivers    */
  41.  
  42. int dtype = -1;                /* current display type        */
  43. char drvname[][8] = {            /* screen resolution names    */
  44.     "CGA", "MONO", "EGA", "VGA"
  45. };
  46. long scadd;                /* address of screen ram    */
  47. int *scptr[NROW];            /* pointer to screen lines    */
  48. unsigned int sline[NCOL];        /* screen line image        */
  49. int egaexist = FALSE;            /* is an EGA card available?    */
  50. int vgaexist = FALSE;            /* is video graphics array available? */
  51. extern union REGS rg;            /* cpu register for use of DOS calls */
  52.  
  53. PASCAL NEAR ibmmove();
  54. PASCAL NEAR ibmeeol();
  55. PASCAL NEAR ibmputc();
  56. PASCAL NEAR ibmeeop();
  57. PASCAL NEAR ibmrev();
  58. PASCAL NEAR ibmcres();
  59. PASCAL NEAR spal();
  60. PASCAL NEAR ibmbeep();
  61. PASCAL NEAR ibmopen();
  62. PASCAL NEAR ibmclose();
  63. PASCAL NEAR ibmkopen();
  64. PASCAL NEAR ibmkclose();
  65. PASCAL NEAR scinit();
  66. int PASCAL NEAR getboard();
  67. PASCAL NEAR egaopen();
  68. PASCAL NEAR egaclose();
  69. PASCAL NEAR fnclabel();
  70.  
  71. #if    COLOR
  72. PASCAL NEAR ibmfcol();
  73. PASCAL NEAR ibmbcol();
  74. int    cfcolor = -1;        /* current forground color */
  75. int    cbcolor = -1;        /* current background color */
  76. int    ctrans[] =        /* ansi to ibm color translation table */
  77.     {0, 4, 2, 6, 1, 5, 3, 7,
  78.      8, 12, 10, 14, 9, 13, 11, 15};
  79. #endif
  80.  
  81. /*
  82.  * Standard terminal interface dispatch table. Most of the fields point into
  83.  * "termio" code.
  84.  */
  85. TERM    term    = {
  86.     NROW-1,
  87.         NROW-1,
  88.         NCOL,
  89.         NCOL,
  90.     MARGIN,
  91.     SCRSIZ,
  92.     NPAUSE,
  93.         ibmopen,
  94.         ibmclose,
  95.     ibmkopen,
  96.     ibmkclose,
  97.         ttgetc,
  98.     ibmputc,
  99.         ttflush,
  100.         ibmmove,
  101.         ibmeeol,
  102.         ibmeeop,
  103.         ibmbeep,
  104.     ibmrev,
  105.     ibmcres
  106. #if    COLOR
  107.     , ibmfcol,
  108.     ibmbcol
  109. #endif
  110. };
  111.  
  112. #if    COLOR
  113. PASCAL NEAR ibmfcol(color)    /* set the current output color */
  114.  
  115. int color;    /* color to set */
  116.  
  117. {
  118.     cfcolor = ctrans[color];
  119. }
  120.  
  121. PASCAL NEAR ibmbcol(color)    /* set the current background color */
  122.  
  123. int color;    /* color to set */
  124.  
  125. {
  126.         cbcolor = ctrans[color];
  127. }
  128. #endif
  129.  
  130. PASCAL NEAR ibmmove(row, col)
  131. {
  132.     rg.h.ah = 2;        /* set cursor position function code */
  133.     rg.h.dl = col;
  134.     rg.h.dh = row;
  135.     rg.h.bh = 0;        /* set screen page number */
  136.     int86(0x10, &rg, &rg);
  137. }
  138.  
  139. PASCAL NEAR ibmeeol()    /* erase to the end of the line */
  140.  
  141. {
  142.     unsigned int attr;    /* attribute byte mask to place in RAM */
  143.     unsigned int *lnptr;    /* pointer to the destination line */
  144.     int i;
  145.     int ccol;    /* current column cursor lives */
  146.     int crow;    /*       row    */
  147.  
  148.     /* find the current cursor position */
  149.     rg.h.ah = 3;        /* read cursor position function code */
  150.     rg.h.bh = 0;        /* current video page */
  151.     int86(0x10, &rg, &rg);
  152.     ccol = rg.h.dl;        /* record current column */
  153.     crow = rg.h.dh;        /* and row */
  154.  
  155.     /* build the attribute byte and setup the screen pointer */
  156. #if    COLOR
  157.     if (dtype != CDMONO)
  158.         attr = (((cbcolor & 15) << 4) | (cfcolor & 15)) << 8;
  159.     else
  160.         attr = 0x0700;
  161. #else
  162.     attr = 0x0700;
  163. #endif
  164.     lnptr = &sline[0];
  165.     for (i=0; i < term.t_ncol; i++)
  166.         *lnptr++ = SPACE | attr;
  167.  
  168.     if (flickcode && (dtype == CDCGA)) {
  169.         /* wait for vertical retrace to be off */
  170.         while ((inp(0x3da) & 8))
  171.             ;
  172.     
  173.         /* and to be back on */
  174.         while ((inp(0x3da) & 8) == 0)
  175.             ;
  176.     }            
  177.  
  178.     /* and send the string out */
  179.     movmem(&sline[0], scptr[crow]+ccol, (term.t_ncol-ccol)*2);
  180.  
  181. }
  182.  
  183. PASCAL NEAR ibmputc(ch) /* put a character at the current position in the
  184.            current colors */
  185.  
  186. int ch;
  187.  
  188. {
  189.     /* if its a newline, we have to move the cursor */
  190.     if (ch == '\n' || ch == '\r') {
  191.         rg.h.ah = 3;
  192.         int86(0x10, &rg, &rg);
  193.         if (rg.h.dh == 24) {
  194.             ibmmove(20, 0);
  195.             /* we must scroll the screen */
  196.             rg.h.ah = 6;    /* scroll up */
  197.             rg.h.al = 1;    /* # of lines to scroll by */
  198.             rg.h.bh = cfcolor; /* attribute for blank line */
  199.             rg.x.cx = 0;    /* upper left corner of scroll */
  200.             rg.x.dx = 0x184f;/* lower right */
  201.             int86(0x10, &rg, &rg);
  202.             rg.h.dh = 23;
  203.         }
  204.         ibmmove(rg.h.dh + 1, 0);
  205.         return;
  206.     }
  207.  
  208.     rg.h.ah = 14;        /* write char to screen with current attrs */
  209.     rg.h.al = ch;
  210. #if    COLOR
  211.     if (dtype != CDMONO)
  212.         rg.h.bl = cfcolor;
  213.     else
  214.         rg.h.bl = 0x07;
  215. #else
  216.     rg.h.bl = 0x07;
  217. #endif
  218.     int86(0x10, &rg, &rg);
  219. }
  220.  
  221. PASCAL NEAR ibmeeop()
  222. {
  223.     int attr;        /* attribute to fill screen with */
  224.  
  225.     rg.h.ah = 6;        /* scroll page up function code */
  226.     rg.h.al = 0;        /* # lines to scroll (clear it) */
  227.     rg.x.cx = 0;        /* upper left corner of scroll */
  228.     rg.x.dx = (term.t_nrow << 8) | (term.t_ncol - 1);
  229.                 /* lower right corner of scroll */
  230. #if    COLOR
  231.     if (dtype != CDMONO)
  232.         attr = ((ctrans[gbcolor] & 15) << 4) | (ctrans[gfcolor] & 15);
  233.     else
  234.         attr = 0;
  235. #else
  236.     attr = 0;
  237. #endif
  238.     rg.h.bh = attr;
  239.     int86(0x10, &rg, &rg);
  240. }
  241.  
  242. PASCAL NEAR ibmrev(state)    /* change reverse video state */
  243.  
  244. int state;    /* TRUE = reverse, FALSE = normal */
  245.  
  246. {
  247.     /* This never gets used under the IBM-PC driver */
  248. }
  249.  
  250. PASCAL NEAR ibmcres(res) /* change screen resolution */
  251.  
  252. char *res;    /* resolution to change to */
  253.  
  254. {
  255.     int i;        /* index */
  256.  
  257.     for (i = 0; i < NDRIVE; i++)
  258.         if (strcmp(res, drvname[i]) == 0) {
  259.             scinit(i);
  260.             return(TRUE);
  261.         }
  262.     return(FALSE);
  263. }
  264.  
  265. PASCAL NEAR spal()    /* reset the pallette registers */
  266.  
  267. {
  268.     /* nothin here now..... */
  269. }
  270.  
  271. PASCAL NEAR ibmbeep()
  272. {
  273. #if    MWC
  274.     ttputc(BEL);
  275. #else
  276.     bdos(6, BEL, 0);
  277. #endif
  278. }
  279.  
  280. PASCAL NEAR ibmopen()
  281. {
  282.     scinit(CDSENSE);
  283.     revexist = TRUE;
  284.         ttopen();
  285. }
  286.  
  287. PASCAL NEAR ibmclose()
  288.  
  289. {
  290. #if    COLOR
  291.     ibmfcol(7);
  292.     ibmbcol(0);
  293. #endif
  294.     /* if we had the EGA open... close it */
  295.     if (dtype == CDEGA || dtype == CDVGA)
  296.         egaclose();
  297.  
  298.     ttclose();
  299. }
  300.  
  301. PASCAL NEAR ibmkopen()    /* open the keyboard */
  302.  
  303. {
  304.     rg.x.ax = 0x3301;
  305.     rg.h.dl = 0x00;
  306.     intdos(&rg, &rg);
  307. }
  308.  
  309. PASCAL NEAR ibmkclose() /* close the keyboard */
  310.  
  311. {
  312. }
  313.  
  314. PASCAL NEAR scinit(type) /* initialize the screen head pointers */
  315.  
  316. int type;    /* type of adapter to init for */
  317.  
  318. {
  319.     union {
  320.         long laddr;    /* long form of address */
  321.         int *paddr;    /* pointer form of address */
  322.     } addr;
  323.     int i;
  324.  
  325.     /* if asked...find out what display is connected */
  326.     if (type == CDSENSE)
  327.         type = getboard();
  328.  
  329.     /* if we have nothing to do....don't do it */
  330.     if (dtype == type)
  331.         return(TRUE);
  332.  
  333.     /* if we try to switch to EGA and there is none, don't */
  334.     if (type == CDEGA && !egaexist)
  335.         return(FALSE);
  336.  
  337.     /* if we try to switch to VGA and there is none, don't */
  338.     if (type == CDVGA && !vgaexist )
  339.         return(FALSE);
  340.  
  341.     /* if we had the EGA or VGA open... close it */
  342.     if (dtype == CDEGA || dtype == CDVGA)
  343.         egaclose();
  344.  
  345.     /* and set up the various parameters as needed */
  346.     switch (type) {
  347.         case CDMONO:    /* Monochrome adapter */
  348.                 scadd = SCADM;
  349.                 newsize(TRUE, 25);
  350.                 maxlines(25);
  351.                 break;
  352.  
  353.         case CDCGA:    /* Color graphics adapter */
  354.                 scadd = SCADC;
  355.                 newsize(TRUE, 25);
  356.                 maxlines(25);
  357.                 break;
  358.  
  359.         case CDEGA:    /* Enhanced graphics adapter */
  360.                 scadd = SCADE;
  361.                 egaopen();
  362.                 newsize(TRUE, 43);
  363.                 maxlines(43);
  364.                 break;
  365.  
  366.         case CDVGA:    /* video graphics array - acts as EGA but more lines */
  367.                 scadd = SCADE;
  368.                 egaopen();
  369.                 newsize(TRUE, 50);
  370.                 maxlines(50);
  371.                 break;
  372.     }
  373.  
  374.     /* reset the $sres environment variable */
  375.     strcpy(sres, drvname[type]);
  376.     dtype = type;
  377.  
  378.     /* initialize the screen pointer array */
  379.     for (i = 0; i < NROW; i++) {
  380.         addr.laddr = scadd + (long)(NCOL * i * 2);
  381.         scptr[i] = addr.paddr;
  382.     }
  383.     return(TRUE);
  384. }
  385.  
  386. /* getboard:    Determine which type of display board is attached.
  387.         Current known types include:
  388.  
  389.         CDMONO    Monochrome graphics adapter
  390.         CDCGA    Color Graphics Adapter
  391.         CDEGA    Extended graphics Adapter
  392.         CDVGA    Vidio Graphics Array
  393.  
  394.         if MONO    set to MONO
  395.            CGA    set to CGA    EGAexist = FALSE VGAexist = FALSE
  396.            EGA    set to CGA    EGAexist = TRUE  VGAexist = FALSE
  397.            VGA    set to CGA    EGAexist = TRUE  VGAexist = TRUE
  398. */
  399.  
  400. int PASCAL NEAR getboard()
  401.  
  402. {
  403.     int type;    /* board type to return */
  404.  
  405.     type = CDCGA;
  406.     int86(0x11, &rg, &rg);
  407.     if ((((rg.x.ax >> 4) & 3) == 3))
  408.         type = CDMONO;
  409.  
  410.     /* test if EGA present */
  411.     rg.x.ax = 0x1200;
  412.     rg.x.bx = 0xff10;
  413.     int86(0x10,&rg, &rg);        /* If EGA, bh=0-1 and bl=0-3 */
  414.     egaexist = !(rg.x.bx & 0xfefc);    /* Yes, it's EGA */
  415.     if (egaexist) {
  416.         /* Adapter says it's an EGA. We'll get the same response
  417.            from a VGA, so try to tell the two apart */
  418.         rg.x.ax = 0x1a00;    /* read display combination */
  419.         int86(0x10,&rg,&rg);
  420.         if (rg.h.al == 0x1a && (rg.h.bl == 7 || rg.h.bl == 8)) {
  421.             /* Function is supported and it's a PS/2 50,60,80 with
  422.                analog display, so it's VGA (I hope!) */
  423.             vgaexist = TRUE;
  424.         } else {
  425.             /* Either BIOS function not supported or something
  426.                other then VGA so set it to be EGA */
  427.             vgaexist = FALSE;
  428.         }
  429.     }
  430.     return(type);
  431. }
  432.  
  433. PASCAL NEAR egaopen()    /* init the computer to work with the EGA or VGA */
  434.  
  435. {
  436.     /* put the beast into EGA 43/VGA 50 line mode */
  437.     rg.x.ax = 3;
  438.     int86(16, &rg, &rg);
  439.  
  440.     rg.h.ah = 17;        /* set char. generator function code */
  441.     rg.h.al = 18;        /*  to 8 by 8 double dot ROM         */
  442.     rg.h.bl = 0;        /* block 0                           */
  443.     int86(16, &rg, &rg);
  444.  
  445.     rg.h.ah = 18;        /* alternate select function code    */
  446.     rg.h.al = 0;        /* clear AL for no good reason       */
  447.     rg.h.bl = 32;        /* alt. print screen routine         */
  448.     int86(16, &rg, &rg);
  449.  
  450.     rg.h.ah = 1;        /* set cursor size function code */
  451.     rg.x.cx = 0x0607;    /* turn cursor on code */
  452.     int86(0x10, &rg, &rg);
  453.  
  454.     outp(0x3d4, 10);    /* video bios bug patch */
  455.     outp(0x3d5, 6);
  456. }
  457.  
  458. PASCAL NEAR egaclose()
  459.  
  460. {
  461.     /* put the beast into 80 column mode */
  462.     rg.x.ax = 3;
  463.     int86(16, &rg, &rg);
  464. }
  465.  
  466. PASCAL NEAR scwrite(row, outstr, forg, bacg)    /* write a line out*/
  467.  
  468. int row;    /* row of screen to place outstr on */
  469. char *outstr;    /* string to write out (must be term.t_ncol long) */
  470. int forg;    /* forground color of string to write */
  471. int bacg;    /* background color */
  472.  
  473. {
  474.     unsigned int attr;    /* attribute byte mask to place in RAM */
  475.     unsigned int *lnptr;    /* pointer to the destination line */
  476.     int i;
  477.  
  478.     /* build the attribute byte and setup the screen pointer */
  479. #if    COLOR
  480.     if (dtype != CDMONO)
  481.         attr = (((ctrans[bacg] & 15) << 4) | (ctrans[forg] & 15)) << 8;
  482.     else
  483.         if (bacg != 0)
  484.             attr = 0x7000;
  485.         else
  486.             attr = 0x0700;
  487. #else
  488.     attr = (((bacg & 15) << 4) | (forg & 15)) << 8;
  489. #endif
  490.     lnptr = &sline[0];
  491.     for (i=0; i<term.t_ncol; i++)
  492.         *lnptr++ = (outstr[i] & 255) | attr;
  493.  
  494.     if (flickcode && (dtype == CDCGA)) {
  495.         /* wait for vertical retrace to be off */
  496.         while ((inp(0x3da) & 8))
  497.             ;
  498.     
  499.         /* and to be back on */
  500.         while ((inp(0x3da) & 8) == 0)
  501.             ;
  502.     }
  503.  
  504.     /* and send the string out */
  505.     movmem(&sline[0], scptr[row],term.t_ncol*2);
  506. }
  507.  
  508. #if    FLABEL
  509. PASCAL NEAR fnclabel(f, n)    /* label a function key */
  510.  
  511. int f,n;    /* default flag, numeric argument [unused] */
  512.  
  513. {
  514.     /* on machines with no function keys...don't bother */
  515.     return(TRUE);
  516. }
  517. #endif
  518. #else
  519. ibmhello()
  520. {
  521. }
  522. #endif
  523.  
  524.